class Solution {
public:
    string countAndSay(int n) {
        string s;
        if (n<1 || n > 30) {
            return s;
        }
        
        s = "1";
        for (int i=1; i<n; i++) {
            string tmp = "";
            int len = s.size()-1;
            int n = 0;
            while(n<=len) {
                char prev = s[n];
                int k = n;
                int cnt = 0;
                while(k<=len) {
                    if (prev == s[k]) {
                        k++;
                        n++;
                        cnt++;
                    } else {
                        break;
                    }
                }
                tmp += (cnt+'0');
                tmp += prev;
            }
            s = tmp;
        }
        return s;
        
    }
};

